# PEDL_5A.R

# Contains: Code to generate Table 3

# Load libraries
library(pacman)
pacman::p_load(dplyr)

# Set working directory (YOUR FILEPATH HERE)
setwd("")

#######################################################################
#######################################################################

# Import data
endline <- readRDS(file = "endline.Rds")

#######################################################################
#######################################################################

# Re-derive arm.factor
endline$arm.factor <- factor(ifelse(endline$arm==4, "0", 
                                    ifelse(endline$arm==1, "125",
                                           ifelse(endline$arm==2, "250", "375"))), 
                             levels = c("0", "125", "250", "375"))

# Descriptives
aggregate( exclusive_max_price ~ arm.factor, endline, mean )
aggregate( exclusive_max_price ~ arm.factor, endline, sd )

endline$trad_freq_daily <- ifelse(endline$trad_freq == 1, 1, 0)
table(endline$arm.factor, endline$trad_freq_daily)

q = c(.25, .5, .75)

endline %>%
  group_by(arm.factor) %>%
  summarize(quant25 = quantile(wood_time_collect, probs = q[1]), 
            quant50 = quantile(wood_time_collect, probs = q[2]),
            quant75 = quantile(wood_time_collect, probs = q[3]))


# Affordable price
smod1 <- aov(exclusive_max_price ~ arm.factor, data = endline)
summary(smod1)
smod1_Tuk <- TukeyHSD(smod1)
smod1_coef <- data.frame(
  Comp = factor(c("125 - 0", "250 - 0", "375 - 0", "250 - 125", "375 - 125", "375 - 250"), 
                levels = c("375 - 250", "375 - 125", "250 - 125", "375 - 0", "250 - 0", "125 - 0")),
  coef = smod1_Tuk$arm.factor[1:6],
  lci = smod1_Tuk$arm.factor[7:12],
  uci = smod1_Tuk$arm.factor[13:18]
)
summary.lm(smod1)